package com.gugee.ins.web.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.gugee.common.web.util.Ret;
import com.gugee.common.web.config.exception.ParamsException;
import com.gugee.ins.web.dto.FollowerSearch;
import com.gugee.ins.web.service.InsRankService;
import com.gugee.common.web.system.promise.Limit;
import com.gugee.common.web.system.promise.RequestLimit;
import com.gugee.common.web.system.user.log.Operation;
import com.xiguaji.boot.toolkit.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "ins排行榜",tags = {"ins排行榜"})
public class InsRankController extends InsBaseController {

    @Autowired
    InsRankService insRankService;

    @ApiOperation(value = "获取INS粉丝排行列表")
    @RequestMapping(value = "fl",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @RequestLimit(value = "InsFollowerRankLimit",property = "page,pageSize",validateType = Limit.VIEW_DATA_LIMIT)
    @Operation(value = "获取INS粉丝排行列表")
    public String getFollowerRankPage(String data) throws UnsupportedEncodingException, ParamsException {
        FollowerSearch search = getParam(FollowerSearch.class, URLDecoder.decode(data,"UTF-8"));
        search.setDateCode(insRankService.getUpdateDateCodeByFollowerRank(search.getCategoryName(),search.getNation()));
        return Ret.success("success",insRankService.getFollowerRankPage(search));
    }

    @ApiOperation(value = "导出INS粉丝排行列表")
    @RequestMapping(value = "fl/export",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "导出INS粉丝排行列表")
    public void exportFollowerRankPage(String data) throws IOException, ParamsException {
        FollowerSearch search = getParam(FollowerSearch.class, URLDecoder.decode(data,"UTF-8"));
        search.setDateCode(insRankService.getUpdateDateCodeByFollowerRank(search.getCategoryName(),search.getNation()));
        val followerRankList = insRankService.getFollowerRankList(search);
        String [] columns = new String[]{"Username","Nickname","Primary Category","Secondary Category","Followers","Follower Growth","Avg Likes","Avg ER"};
        List<Map<String,Object>> rows = new ArrayList<>();

        followerRankList.forEach( f ->{
            Map<String,Object> row = new LinkedHashMap<>();
            row.put(columns[0],f.getUsername());
            row.put(columns[1],f.getFullName());
            row.put(columns[2],f.getCategoryName());
            row.put(columns[3],f.getSecondCategory());
            row.put(columns[4],f.getFollowerCount());
            row.put(columns[5],f.getFollowerGrow());
            row.put(columns[6],f.getAvgLike());
            row.put(columns[7],f.getAvgEr());
            rows.add(row);
        });
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.write(rows,true);
        HttpUtil.getResponse().setContentType("application/vnd.ms-excel;charset=utf-8");
        HttpUtil.getResponse().setHeader("Content-Disposition","attachment;filename="+Integer.parseInt(DateUtil.format(LocalDateTime.now(),"yyyyMMdd"))+".xls");
        writer.flush(HttpUtil.getResponse().getOutputStream());
        writer.close();

    }
}
